package com.jml.原子引用;


import static com.jml.原子引用.ABADemo.atomicReference;

import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @author:jml
 * @date: 2019/4/29 11:06
 * @description：
 */
public class ABAResolveDemo {
    static AtomicStampedReference<Integer> atomicStampedReference = new AtomicStampedReference<>(100,1);
    public static void main(String[] args) {
        new Thread(()->{
            //得到版本号
            int stamp = atomicStampedReference.getStamp();
            System.out.println(Thread.currentThread().getName()+"第一次版本号："+stamp);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            atomicStampedReference.compareAndSet(100,101,stamp,stamp+1);
            System.out.println(Thread.currentThread().getName()+"第二次版本号："+stamp+1);
            stamp = atomicStampedReference.getStamp();
            atomicStampedReference.compareAndSet(101,100,stamp,stamp+1);
            System.out.println(Thread.currentThread().getName()+"第三次版本号："+stamp+1);
        },"t1").start();

        new Thread(()->{
            int stamp = atomicStampedReference.getStamp();
            System.out.println(Thread.currentThread().getName()+"版本号："+stamp);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            boolean b = atomicStampedReference.compareAndSet(100, 2019, stamp, stamp + 1);
            System.out.println(b+" :  "+atomicReference.get());
        },"t2").start();
    }
}
